package com.crazypos.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazypos.common.ApiRestResponse;
import com.crazypos.common.utils.SecurityUtil;
import com.crazypos.pojo.BrandEntity;
import com.crazypos.pojo.bo.CreateBrandBO;
import com.crazypos.pojo.bo.UpdateBrandBO;
import com.crazypos.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/brand")
public class AdminBrandController {

    @Autowired
    private IBrandService brandService;

    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse create(@RequestBody @Validated CreateBrandBO bo) {
        String currentUserId = SecurityUtil.getUserId();
        brandService.createBrand(true, bo, currentUserId, "");
        return ApiRestResponse.success();
    }

    @PostMapping("/update")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse update(@RequestBody @Validated UpdateBrandBO bo) {
        String currentUserId = SecurityUtil.getUserId();
        brandService.updateBrand(true, bo, currentUserId, "");
        return ApiRestResponse.success();
    }

    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse list(
            @DefaultValue("1") Integer startPage,
            @DefaultValue("10") Integer pageSize,
            @DefaultValue("") String name) {

        IPage<BrandEntity> result = brandService.getBrandList(startPage,pageSize,name,true);
        return ApiRestResponse.success(result);
    }

}
